package com.situ.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.situ.entity.Food;
import com.situ.service.FoodService;
import com.situ.util.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.List;

@RestController
public class Foodcontroller {
    @Autowired
    FoodService service;

    @RequestMapping("query")
    public ResponseData query(@RequestParam(defaultValue ="1",required = false) int startPage ,
                            @RequestParam(defaultValue = "5",required = false) int PageSize){
        try {
            PageHelper.startPage(startPage, PageSize);
            List<Food> list = service.queryall();
            PageInfo pageInfo = new PageInfo<Food>(list, 5);
            return ResponseData.ok().put("list", list).put("pageInfo", pageInfo);
        }catch (Exception e){
            return ResponseData.error500().put("mess", "意外错误！！");
        }
    }


    //取配置文件中配置的虚拟路径
    @Value("${path1}")
    String path;


    @RequestMapping("save")
    public ResponseData insert(MultipartFile files, Food food){
        try{
            if(files!=null){
                String file1FileName = files.getOriginalFilename();
                //使用当前时间毫秒数 做为文件名称
                file1FileName= System.currentTimeMillis()+"id"+file1FileName.substring(file1FileName.lastIndexOf("."),file1FileName.length());
                //保存文件的地址
                File file=new File(path+"/images",file1FileName);

                //开始接收    获得读文件的流
                InputStream is = files.getInputStream();

                FileOutputStream fos=new FileOutputStream(file);
                byte [] b=new byte[1024*100];  //开辟一个空间 100KB
                int len=0; //len 的到实际接收的大小
                while(  (len=is.read(b))>0  ){
                    fos.write(b, 0, len);
                }
                fos.close();
                is.close();
                //成功为200
                //把晚间的名称 放到对象里面
                food.setPic(file1FileName);
            }
            service.save(food);
            return ResponseData.ok();
        }catch (Exception e){
            return ResponseData.error500();
        }
    }

    @RequestMapping("dl/{id}")
    public ResponseData dlf(@PathVariable Integer id){
        try{
            service.dl(id);
            return ResponseData.ok();
        }catch (Exception e){
            return ResponseData.error500();
        }
    }

    @RequestMapping("updatebyid/{id}")
    public ResponseData updatebyid(@PathVariable  int id){
        try {
            Food food = service.querybyid(id);
            return ResponseData.ok().put("food", food);
        }catch (Exception e){
            return ResponseData.error500();
        }
    }

    @RequestMapping("updatefood")
    public ResponseData updatefood( MultipartFile files,Food food){
        try{
            if(files!=null){
                String file1FileName = files.getOriginalFilename();
                //使用当前时间毫秒数 做为文件名称
                file1FileName= System.currentTimeMillis()+"id"+file1FileName.substring(file1FileName.lastIndexOf("."),file1FileName.length());
                //保存文件的地址
                File file=new File(path+"/images",file1FileName);

                //开始接收    获得读文件的流
                InputStream is = files.getInputStream();

                FileOutputStream fos=new FileOutputStream(file);
                byte [] b=new byte[1024*100];  //开辟一个空间 100KB
                int len=0; //len 的到实际接收的大小
                while(  (len=is.read(b))>0  ){
                    fos.write(b, 0, len);
                }
                fos.close();
                is.close();
                //成功为200
                //把晚间的名称 放到对象里面
                food.setPic(file1FileName);
            }
            service.updatefood(food);
            return ResponseData.ok();
        }catch (Exception e){
            return ResponseData.error500();
        }
    }
}
